using System;
using UnityEngine;
using QFramework;
using UnityEngine.UI;

namespace Survivor
{
    public partial class FloatingTextController : ViewController
    {
        private static FloatingTextController instance;

        private void Awake()
        {
            instance = this;
        }

        void Start()
        {
            FloatTextPrefab.Hide();
        }

        public static void PlayFloatingText(Vector2 targetPos, string value)
        {
            instance.FloatTextPrefab.InstantiateWithParent(instance.transform)
                .Position(targetPos)
                .Self(rectTransform =>
                {
                    var floatingTxt = rectTransform.Find("Text").GetComponent<Text>();
                    floatingTxt.text = value;
                    var posY = targetPos.y;
                    ActionKit.Sequence()
                        .Lerp(0, 0.5f, 0.5f, p =>
                        {
                            rectTransform.PositionY(posY + p * 0.5f);
                            floatingTxt.LocalScaleX(Mathf.Clamp01(p * 8));
                            floatingTxt.LocalScaleY(Mathf.Clamp01(p * 8));
                        })
                        .Delay(.5f)
                        .Lerp(1.0f, 0, 0.3f, p => { floatingTxt.ColorAlpha(p); },
                            () => floatingTxt.DestroyGameObjGracefully())
                        .Start(floatingTxt);
                })
                .Show();
        }
    }
}